import { pAjax } from "../../index/javascripts/gather";
//获取元素
const form = document.querySelector('form');
const username = document.querySelector('.username')
const password = document.querySelector('.password')
const errBox = document.querySelector('.errBox');


//清除默认样式
form.addEventListener('submit', async function (e) {
    e.preventDefault();

    //获取用户输出的内容
    const nameBox = username.value;
    const wordBox = password.value;

    let res = await pAjax({
        url:'http://localhost:8888/users/login',
        method:'post',
        data:{
            username:username.value,
            password:password.value
        },
        dataType:'json'
    })
    errBox.style.display = 'none'
    if(res.code !==1){
        errBox.style.display = 'block'
    }
    console.log(res);
    window.localStorage.setItem('id',res.user.id)
    window.localStorage.setItem('token',res.token)

    window.location.href = './index.html';

    if (!nameBox || !wordBox)return
})

